package com.xyy.saas.payment.callback.core.handler;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.adpater.pingxx.domain.WithdrawalDO;
import com.xyy.saas.payment.callback.core.result.CallbackRunner;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.vo.CallbackResultVo;
import com.xyy.saas.payment.dao.mapper.WithdrawOrderMapper;
import com.xyy.saas.payment.dao.model.WithdrawOrder;
import com.xyy.saas.payment.util.JSONUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wangtianqi
 * @date 2020/10/26 10:08
 */
@Component
public class WithdrawalHandler implements BaseHandler {
    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(WithdrawalHandler.class);

    @Autowired
    private WithdrawOrderMapper withdrawOrderMapper;
    @Autowired
    private CallbackRunner callbackRunner;

    @Override
    public String getMethod() {
        return Constants.METHOD_WITHDRAW_NAME;
    }

    @Override
    public boolean callBackHandler(Object domain) {
        LOGGER.info("WithdrawalHandler#callBackHandler withdrawalDO:{}", JSON.toJSONString(domain));
        WithdrawalDO withdrawalDO = (WithdrawalDO) domain;

        if(StringUtils.isEmpty(withdrawalDO.getId())) {
            LOGGER.error("WithdrawalHandler#callBackHandler id is empty error withdrawalDO:{} ", JSON.toJSONString(withdrawalDO));
            return true;
        }

        WithdrawOrder withdrawOrder = withdrawOrderMapper.selectByWithdrawNo(withdrawalDO.getWithdrawNo());

        // 避免重复处理
        if(("succeeded".equals(withdrawalDO.getStatus()) && WithdrawOrder.STATUS_FINISH.equals(withdrawOrder.getStatus()))
            || ("fail".equals(withdrawalDO.getStatus()) && WithdrawOrder.STATUS_FAIL.equals(withdrawOrder.getStatus()))
            || ("refund".equals(withdrawalDO.getStatus()) && WithdrawOrder.STATUS_REFUND.equals(withdrawOrder.getStatus()))) {
            LOGGER.info("WithdrawalHandler#callBackHandler withdraw order status is finish. withdrawalDO:{}", JSONUtils.toJSON(withdrawalDO));
            return true;
        }

        if("succeeded".equals(withdrawalDO.getStatus())) {
            // 提现成功
            withdrawOrder.setStatus(WithdrawOrder.STATUS_FINISH);
            withdrawOrder.setSuccessTime(withdrawalDO.getTimeSucceeded());
            withdrawOrder.setUpdateTime(new Date());
            Long unixTime = withdrawOrder.getSuccessTime().getTime() / 1000;

            // 异步回调
            if(StringUtils.isNotEmpty(withdrawOrder.getCallBackUrl())) {
                callbackRunner.callback(withdrawOrder.getWithdrawNo(), CallbackResultVo.TYPE_WITHDRAW_SUCCESS, withdrawOrder.getAmount(), withdrawOrder.getFee(), unixTime, withdrawOrder.getCallBackUrl(), null, null, null,  withdrawOrder.getBusinessWithdrawNo());
            }
        }
        else if("refund".equals(withdrawalDO.getStatus())) {
            // 退汇
            LOGGER.info("WithdrawalHandler#callBackHandler status is refund withdrawalDO:{}", JSON.toJSONString(withdrawalDO));
            withdrawOrder.setStatus(WithdrawOrder.STATUS_REFUND);
            withdrawOrder.setUpdateTime(new Date());

            // 异步回调
            if(StringUtils.isNotEmpty(withdrawOrder.getCallBackUrl())) {
                callbackRunner.callback(withdrawOrder.getWithdrawNo(), CallbackResultVo.TYPE_WITHDRAW_REFUND, withdrawOrder.getAmount(), withdrawOrder.getFee(), 0L, withdrawOrder.getCallBackUrl(),null, withdrawalDO.getFailureMsg(), null, withdrawOrder.getBusinessWithdrawNo());
            }
        }
        else {
            LOGGER.info("WithdrawalHandler#callBackHandler status isn't succeeded withdrawalDO:{}", JSON.toJSONString(withdrawalDO));
            withdrawOrder.setStatus(WithdrawOrder.STATUS_FAIL);
            withdrawOrder.setUpdateTime(new Date());

            // 异步回调
            if(StringUtils.isNotEmpty(withdrawOrder.getCallBackUrl())) {
                callbackRunner.callback(withdrawOrder.getWithdrawNo(), CallbackResultVo.TYPE_WITHDRAW_FAIL, withdrawOrder.getAmount(), withdrawOrder.getFee(), 0L, withdrawOrder.getCallBackUrl(),null, withdrawalDO.getFailureMsg(), null, withdrawOrder.getBusinessWithdrawNo());
            }
        }

        int count = withdrawOrderMapper.updateByPrimaryKey(withdrawOrder);

        if(count < 1) {
            LOGGER.error("WithdrawalHandler#callBackHandler update status error withdrawalDO:{}", JSON.toJSONString(withdrawalDO));
        }

        return true;
    }
}